package com.heima.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.mapper.ApArticleContentMapper;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.ApArticleFreemarkerService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.article.vos.HotArticleVo;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {


    @Autowired(required = false)
    private ApArticleMapper apArticleMapper;


    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Autowired
    private ApArticleFreemarkerService apArticleFreemarkerService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 文章列表查询plus.查询的就是首页
     *
     * @param dto ,推荐频道  __all__  如果其他频道则是id
     * @param
     * @return
     */
    @Override
    public ResponseResult loadArticleList2(ArticleHomeDto dto) {
        String hotArticleListStr = stringRedisTemplate.opsForValue().get(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + dto.getTag());
        //转化类型
        List<HotArticleVo> hotArticleVoList = JSONArray.parseArray(hotArticleListStr, HotArticleVo.class);

        return ResponseResult.okResult(hotArticleVoList);
    }

    /**
     * 保存文章内容+文章配置+文章信息
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult save(ArticleDto dto) {
        //1.非空判断
        if (dto.getContent() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2判断id 若不存在,则表示新增
        ApArticle apArticle = new ApArticle();
        if (dto.getId() == null) {
            //2.1.拷贝属性，dto 到 ap_article
            BeanUtils.copyProperties(dto, apArticle);
            //2.2调用mp新增ap_article
            apArticleMapper.insert(apArticle);
            //2.3新增ap_article_config
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigMapper.insert(apArticleConfig);
            //3.新增ap_article_content
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentMapper.insert(apArticleContent);
        } else {
            //3若存在，表修改
            //3.1先根据id去查询文章，后修改
            apArticle = apArticleMapper.selectById(dto.getId());
            //3.2判断查询文章存在，更新数据库
            if (apArticle != null) {
                BeanUtils.copyProperties(dto, apArticle);
                apArticleMapper.updateById(apArticle);
                //3.3先查文章内容，后修改
                ApArticleContent apArticleContent = apArticleContentMapper.selectOne(Wrappers.<ApArticleContent>lambdaQuery()
                        .eq(ApArticleContent::getArticleId, apArticle.getId()));

                apArticleContent.setContent(dto.getContent());
                apArticleContentMapper.updateById(apArticleContent);
            }
        }

        //异步调用 生成静态文件上传到minio中
        apArticleFreemarkerService.buildArticleToMinIO(apArticle,dto.getContent());

        //4.返回文章id
        return  ResponseResult.okResult(apArticle == null ? "" : apArticle.getId());
    }

    /**
     * 根据参数加载文章列表
     *
     * @param articleHomeDto
     * @return
     */
    @Override
    public ResponseResult load(Short loadType, ArticleHomeDto articleHomeDto) {
        //1.非空判断
        if (articleHomeDto.getSize() == null || articleHomeDto.getSize() == 0) {
            articleHomeDto.setSize(10);
        }
        if (StringUtils.isEmpty(articleHomeDto.getTag())) {
            articleHomeDto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        if (articleHomeDto.getMaxBehotTime() == null) {
            articleHomeDto.setMaxBehotTime(new Date());
        }
        if (articleHomeDto.getMinBehotTime() == null) {
            articleHomeDto.setMinBehotTime(new Date());
        }
        //2.调用mapper层加载文章

        //3.返回结果

        return ResponseResult.okResult(apArticleMapper.load(loadType, articleHomeDto));
    }
}
